package org.rcy.framework.thrift.metadata;

import org.apache.thrift.TFieldIdEnum;
import org.apache.thrift.TFieldRequirementType;

import java.lang.reflect.Type;

public class MetaDataBuilder {

	/**
	 * 创建FieldMetaData
	 */
	public static FieldMetaData fieldMetaData(TFieldIdEnum field, Type javaType, Type[] typeArguments){
		FieldValueMetaData valueMetaData = valueMetaData(javaType,typeArguments);
		return new FieldMetaData(field, TFieldRequirementType.DEFAULT, valueMetaData);
	}

	/**
	 * 创建FieldValueMetaData对象
	 * @param javaType Type
	 * @return
	 * @throws TException 
	 */
	public static FieldValueMetaData valueMetaData(Type javaType,Type[] typeArguments){
		byte type = ThriftType.getType(javaType);
		
		JavaType javaTypeEnum =JavaType.determine(javaType);

		switch (javaTypeEnum) {

		case LIST:
			// List类型必须添加泛型
			return new ListMetaData(javaType,typeArguments);

		case SET:
			// SET类型必须添加泛型
			return new SetMetaData(javaType,typeArguments);

		case MAP:
			// Map类型参数的Key和Value都需要添加泛型
			return new MapMetaData(javaType,typeArguments);

		case ENUM:
			return new EnumMetaData(javaType);

		case STRUCT:
			return new StructMetaData(javaType);

		default:
			return new FieldValueMetaData(type, javaType);
		}
	}

}